package com.siae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.ItemTypeMapper;
import com.siae.pojo.ItemType;
import com.siae.pojo.MessageTemplate;
import com.siae.pojo.vo.Result;
import com.siae.service.ItemTypeService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ItemTypeServiceImpl extends ServiceImpl<ItemTypeMapper, ItemType> implements ItemTypeService {
    @Autowired
    private ItemTypeMapper itemTypeMapper;

    @Override
    public Result<List<ItemType>> getAllItemTypes(String name, String description) {
        try{
            QueryWrapper<ItemType> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(name),"name",name)
                    .like(StringUtils.isNotBlank(description),"description",description);
            List<ItemType> list = itemTypeMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询项目类型列表失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<ItemType> getItemTypeById(Integer id) {
        try{
            ItemType itemType = itemTypeMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,itemType);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询项目类型失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<ItemType> getItemTypeByName(String name) {
        try{
            if(StringUtils.isBlank(name)){
                return Result.fail(BizCode.BLANK_ERROR);
            }
            QueryWrapper<ItemType> wrapper = new QueryWrapper<>();
            wrapper.eq(StringUtils.isNotBlank(name),"name",name);
            ItemType itemType = itemTypeMapper.selectOne(wrapper);
            return Result.success(BizCode.SUCCESS,itemType);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询项目类型失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result addItemType(ItemType type) {
        try{
            type.setCreatedAt(new Date());
            type.setUpdatedAt(new Date());
            type.setIsDel(0);
            itemTypeMapper.insert(type);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("添加项目类型失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result updateItemType(ItemType type) {
        try{
            type.setUpdatedAt(new Date());
            itemTypeMapper.updateById(type);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("更新项目类型失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result deleteItemType(Integer id) {
        try{
            itemTypeMapper.deleteById(id);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除项目类型失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }
}
